import 'package:flutter/material.dart';
import 'package:flutter_hi_cache/flutter_hi_cache.dart';
import 'package:get/get.dart';
import 'package:yysk_app/pages/login/login_controller.dart';
import 'package:yysk_app/pages/login/login_page.dart';
import 'package:yysk_app/pages/main/main_page.dart';
import 'package:yysk_app/routes/global_controller.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';

class WelcomePage extends StatefulWidget {
  static const path = '/welcome';
  const WelcomePage({super.key});

  @override
  State<WelcomePage> createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> {
  final GlobalController controller = Get.find();

  @override
  void initState() {
    super.initState();
    // consoleLog('初始化完成');
    Future.delayed(const Duration(milliseconds: 2000), () {
       dynamic inviteCode = Get.parameters['inviteCode'] ?? Get.arguments?['inviteCode'];

      //有邀请码的时候。直接去注册
      if (inviteCode != null) {
        // Get.offAllNamed('${RegisterPage.path}?inviteCode=$inviteCode',
        //     arguments: {'inviteCode': inviteCode});
        //     return;
        return;
      }
       else if (HiCache.getInstance().get(LoginController.boardingPassToken) ==
          null) {
       Get.offAllNamed(LoginPage.path);
      }
      else {
         controller.getUserInfo(
            success: (result) {
              if (result.code == 200) {
                Get.offAllNamed(MainPage.path);
                controller.getStaticData();
              }
            },
            fail: () {
              Get.offAllNamed(LoginPage.path);
            },
          );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: HiCache.preInit(),
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          consoleLog('初始化完成');
          return Scaffold(
            resizeToAvoidBottomInset: false,
            body: Stack(
              children: [
                Positioned.fill(
                  child: Image.asset('static/images/bg2.png', fit: BoxFit.cover),
                ),
                Positioned(
                  top: 200.px,
                  left: 109.px,
                  child: ImageAssetWidget(
                    imageUrl: 'static/images/box/logo.png',
                    width: 158.px,
                    height: 158.px,
                  ),
                ),
                //  Positioned(
                //    bottom: 91.px,
                //   left: 140.px,
                //   child: ImageAssetWidget(
                //     imageUrl: 'static/images/box/txt.png',
                //     width: 95.px,
                //     height: 26.px,
                //   ),
                // ),
                 Positioned(
                  bottom: 72.px,
                  left: 122.px,
                  child: ImageAssetWidget(
                    imageUrl: 'static/images/box/txt3.png',
                    width: 130.px,
                    height:57.px,
                  ),
                ),
              ],
            ),
          );
        } else {
          return const Scaffold(
            body: Center(child: CircularProgressIndicator()),
          );
        }
      },
    );
  }
}
